package com.hy.thread;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.hy.config.ConfigurationUtils;
import com.hy.utils.CommonUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThreadPoolService {
	protected static final Logger logBase = LoggerFactory.getLogger(ThreadPoolService.class);
	private static int threadPoolSize= Integer.parseInt(ConfigurationUtils.getString("threadPoolSize","20"));//线程池大小
//	private static ThreadPoolExecutor logThreadPool=new ThreadPoolExecutor(threadPoolSize, threadPoolSize,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());

	private static ThreadPoolExecutor commonThreadPool=new ThreadPoolExecutor(threadPoolSize, threadPoolSize,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
	private static ThreadPoolExecutor pushThreadPool=new ThreadPoolExecutor(threadPoolSize, threadPoolSize,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
	private static ScheduledThreadPoolExecutor scheduledThreadPool=new ScheduledThreadPoolExecutor(threadPoolSize);
	
	
//	/**
//	 * 添加线程到日志线程池中
//	 * @param command
//	 */
//	public static void addThreadToLogPool(Runnable command){
//		try{
//			long st=System.currentTimeMillis();
//			logThreadPool.execute(command);
//			logBase.info("ThreadPoolService.addThreadToLogPool.Info:-------------add thread("+command.getClass().getName()+") to logThreadPool success,usedTime="+(System.currentTimeMillis()-st)+"ms,logThreadPool.getPoolSize()="+logThreadPool.getPoolSize()+",logThreadPool.getActiveCount()="+logThreadPool.getActiveCount()+",logThreadPool.getCompletedTaskCount()="+logThreadPool.getCompletedTaskCount()+",logThreadPool.getQueue().size()="+logThreadPool.getQueue().size());
//		}catch(Exception e){
//			logBase.error("ThreadPoolService.addThreadToLogPool.Exception:",e);
//		}
//	}
	/**
	 * 关闭线程池
	 */
	public static void shutdown(){
		try{
			pushThreadPool.shutdownNow();
			logBase.info("ThreadPoolService.shutdown.Info:----------pushThreadPool is shutdown");
			scheduledThreadPool.shutdownNow();
			logBase.info("ThreadPoolService.shutdown.Info:----------scheduledThreadPool is shutdown");
		}catch(Exception e){
			logBase.error("ThreadPoolService.shutdown.Exception:",e);
		}

	}
	
	/**
	 * 添加线程到公用线程池中
	 * @param command
	 */
	public static void addThreadToCommonPool(Runnable command){
		try{
			long st=System.currentTimeMillis();
			commonThreadPool.execute(command);
			logBase.info("ThreadPoolService.addThreadToCommonPool.Info:-------------add thread("+command.getClass().getName()+") to commonThreadPool success,usedTime="+(System.currentTimeMillis()-st)+"ms,commonThreadPool.getPoolSize()="+commonThreadPool.getPoolSize()+",commonThreadPool.getActiveCount()="+commonThreadPool.getActiveCount()+",commonThreadPool.getCompletedTaskCount()="+commonThreadPool.getCompletedTaskCount()+",commonThreadPool.getQueue().size()="+commonThreadPool.getQueue().size());
		}catch(Exception e){
			logBase.error("ThreadPoolService.addThreadToCommonPool.Exception:",e);
		}
	}
	
//	/**
//	 * 添加线程到推送线程池中
//	 * @param command
//	 */
//	public static void addThreadToPushPool(Runnable command){
//		try{
//			long st=System.currentTimeMillis();
//			pushThreadPool.execute(command);
//			logBase.info("ThreadPoolService.addThreadToPushPool.Info:-------------add thread("+command.getClass().getName()+") to pushThreadPool success,usedTime="+(System.currentTimeMillis()-st)+"ms,pushThreadPool.getPoolSize()="+pushThreadPool.getPoolSize()+",pushThreadPool.getActiveCount()="+pushThreadPool.getActiveCount()+",pushThreadPool.getCompletedTaskCount()="+pushThreadPool.getCompletedTaskCount()+",pushThreadPool.getQueue().size()="+pushThreadPool.getQueue().size());
//		}catch(Exception e){
//			logBase.error("ThreadPoolService.addThreadToPushPool.Exception:",e);
//		}
//	}
	/**
	 * 添加线程到计划线程池中
	 * @param command
	 * @param scheduleTime 计划执行时间
	 */
	public static void addThreadToScheduledPool(Runnable command,Date scheduleTime){
		try{
			long st=System.currentTimeMillis();
			scheduledThreadPool.schedule(command, scheduleTime.getTime()-st<0?0:scheduleTime.getTime()-st, TimeUnit.MILLISECONDS);
			logBase.info("ThreadPoolService.addThreadToScheduledPool.Info:-------------add thread("+command.getClass().getName()+") to scheduledThreadPool success,scheduleTime="+ CommonUtility.getDateFormat().format(scheduleTime)+",usedTime="+(System.currentTimeMillis()-st)+"ms,scheduledThreadPool.getPoolSize()="+scheduledThreadPool.getPoolSize()+",scheduledThreadPool.getActiveCount()="+scheduledThreadPool.getActiveCount()+",scheduledThreadPool.getCompletedTaskCount()="+scheduledThreadPool.getCompletedTaskCount()+",scheduledThreadPool.getQueue().size()="+scheduledThreadPool.getQueue().size());
		}catch(Exception e){
			logBase.error("ThreadPoolService.addThreadToScheduledPool.Exception:",e);
		}
	}
}
